#array.py
#Create arrays for tenure,cohort,namespace # of edits plot
#Author: Nate Leech
#Author: Guanyu Wang

#Input path and cohort, outputs arrays in sys.out

import sys
import collections


def main():
	colorList = ["darkorange4","red","forestgreen","black","orange","deepskyblue3","darkmagenta","green"]
	maximum = 0
	tenureDict = collections.defaultdict(list)
	path = sys.argv[1]
	year = sys.argv[2]
	f = open(path,"r")
	namespace = path.split('/')[-1].replace(".txt","")
	for line in f:
		tokens = line.split(' ')
		if tokens[0] == "Stats":
			continue
		if year in tokens[0]:
			tenureDict[tokens[0][5:6]].append(float(tokens[1].replace('\n','')))
	for y in sorted(tenureDict.keys()):	
		maximum = max(maximum,max(tenureDict[y]))
		aList = []
		for item in tenureDict[y]:
			aList.append(str(item))
		
		print "tenure"+ y + " <- c(" + ",".join(aList) + ')'
        print 'g_range <- range(0,'+ str(maximum) +')'
	print 'png("'+namespace+" "+year+'.png")'
	print 'plot(tenure' + str(len(tenureDict.keys())-1) + ', type="o"' +',col="blue",xaxt="n",ylim=g_range,ann=FALSE)'
        print 'axis(1, at=1:'+str(2012-int(year))+',lab=c("2004","2005","2006","2007","2008","2009","2010","2011+"))'    #add + sign
        print "lines(tenure0"+',type="o",col="'+colorList[0]+'",pch=16)'
	for x in range(1,len(tenureDict.keys())-1):
		print "lines(tenure"+str(x)+',type="o",col="'+colorList[x]+'")'
        print "box()"
        print 'title(main="' + namespace.title() + ' Namespace, Start Year: ' + year +'",' + 'col.main="black", font.main=4)'
        print 'title(xlab="Offset Year", col.xlab="black")\ntitle(ylab="Number of Hours", col.ylab="black")\ndev.off()'  #add legend


main()
